﻿using System;
using System.Collections.Generic;

using System.Text;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Radio button control, which is rendered as an INPUT tag with a type of RADIO.
	/// </summary>
	public class RadioButton : BaseToggleControl
	{
		#region PUBLIC
		#region INIT
		#endregion
		#region EVENTS
		#endregion
		#region PROPS
		/// <summary>
		/// The ID of the RadioGroup control to which this RadioButon is assigned.
		/// </summary>
		public string GroupId { get; set; }
		/// <summary>
		/// The RadioGroup control to which this RadioButon is assigned.
		/// </summary>
		public RadioGroup Group
		{
			get
			{
				return this.FindControlGlobal(GroupId) as RadioGroup;
			}
		}
		#endregion
		#region METHODS
		#endregion
		#endregion
		#region INTERNAL
		#region CONST/STATIC
		#endregion
		#region VARS
		#endregion
		#region PROPS
		#endregion
		#region METHODS
		#region INIT
		/// <summary>
		/// Initializes the control: registers with the group.
		/// </summary>
		/// <param name="e"></param>
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			if (string.IsNullOrEmpty(this.GroupId))
				throw new InvalidOperationException(string.Format("Radio button '{0}' needs a group ID. Radio buttons are useless without a group.", ID));

			if (this.Group == null)
				throw new InvalidOperationException(string.Format("Radio group '{0}' not found for radio button '{1}'.", GroupId, ID));

			this.Group.Register(this);

			this.ValueChanged.CopyFrom(this.Group.ValueChanged);
			//if (!string.IsNullOrEmpty(this.Group.ValueChanged.Client))
			//  this.ValueChanged.Client = this.Group.ValueChanged.Client;
			//if(this.Group.ValueChanged.A
		}
		#endregion
		#region DATA
		#endregion
		#region HANDLERS
		#endregion
		#region RENDER
		/// <summary>
		/// Adds the type and name attributes.
		/// </summary>
		/// <param name="writer"></param>
		protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
		{
			writer.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Type, "radio");
			writer.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Name, this.Group.UniqueID);

			base.AddAttributesToRender(writer);
		}
		/// <summary>
		/// Adds the value attribute which is used to determine which one of the radio buttons is selected.
		/// </summary>
		/// <param name="writer"></param>
		protected override void AddValueAttributes(System.Web.UI.HtmlTextWriter writer)
		{
			base.AddValueAttributes(writer);

			writer.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Value, ID);

		}
		#endregion
		#endregion
		#endregion

	}
}
